/*
 * @Title: 收货地址列表页
 * @Author: zhj
 * @Date:   2018-10-15
 * @Last Modified by:   JSL
 * @Last Modified time: 2018-11-15
 */
var dataInfo = new Object(),
  template = {
    init: function() {
      dataInfo.init = new Vue({
        el: "#app",
        data: {
          id: "",
          isDefault: "", //删除是否是默认地址
          addressLists: [], //全部地址
          buyWay: getQueryString('buyWay'), //购买渠道
          urlFrom: getQueryString('from'), //地址来源
        },
        methods: {
          onLoad: function() {
            this.getAddressByUser();
          },
          getAddressByUser: function() { //查询用户全部收货地址
            ajaxTemplate(addressTest + SERVICE.getAddressByUser, function(data) {
              var daTa = data.data.result;
              console.log(daTa);
              if (data.tag == 0) {
                dataInfo.init.addressLists = daTa;
                dataInfo.init.isDefault = daTa[0]['defaultAddress'];
              };
            }, {
              userId: userId,
            });
          },
          isdelect: function(index) { //设为默认地址
            this.id = this.addressLists[index]["id"];
            ajaxTemplate(addressTest + SERVICE.updDefaultAddress, function(data) {
              if (data.tag == 0) {
                dataInfo.init.onLoad();
              };
            }, {
              userId: userId,
              id: this.id
            });
          },
          toEditAddress: function(index) { //跳转编辑地址页
            this.id = this.addressLists[index]["id"];
            location.href = '../html/editAddress.html?from=' + this.urlFrom + "&addressId=" + this.id + '&buyWay=' + this.buyWay;
          },
          toAddress: function() { //跳转添加新地址页
            location.href = '../html/address.html?from=' + this.urlFrom + '&buyWay=' + this.buyWay;
          },
          toOrderfilling: function(index) { //跳转订单提交页
            this.id = this.addressLists[index]["id"];//181115 增加拼团跳转验证
            if (this.buyWay == 3) {
              location.href = '../html/' + this.urlFrom + '?addressId=' + this.id + '&' + unescape(getCookie('zmtMallPtOrder'));
            } else {
              location.href = '../html/' + this.urlFrom + '?addressId=' + this.id + '&buyWay=' + this.buyWay;
            };
          },
          delectAddress: function(index) {
            this.$dialog.confirm({
              message: '确认删除该地址吗？'
            }).then(() => { //确认
              this.id = this.addressLists[index]["id"];
              ajaxTemplate(addressTest + SERVICE.delAddress, function(data) {
                if (data.tag == 0) {
                  dataInfo.init.delect(index);
                  dataInfo.init.getAddressByUser();
                } else {
                  dataInfo.init.$toast(data.errMsg);
                };
              }, {
                userId: userId,
                id: this.id,
                isDefault: this.isDefault
              });

            });
          },
          delect:function(index) { //删除地址方法
            this.addressLists.splice(index, 1);
          }
        }
      });
      dataInfo.init.onLoad();
    }
  };
template.init();
